using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using MsExchangeBcsConnector;

namespace MsExchangeBcsConnector.Features.EmlFormatlHandler
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("3c136f83-8cd8-4993-a4e2-7389b2716e21")]
    public class EmlFormatlHandlerEventReceiver : SPFeatureReceiver
    {

        #region Constant
        private const string FeatureId = "768b2395-237f-427f-9915-6783b77b81f4";
        private static string handlerPath = "*.eml";//"*.eml";        
        #endregion

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPWebConfigModification mod in CreateConfigModifications())
            {
                webApp.WebConfigModifications.Add(mod);
            }

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }




        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPWebConfigModification mod in CreateConfigModifications())
            {
                webApp.WebConfigModifications.Remove(mod);
            }

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }



        private List<SPWebConfigModification> CreateConfigModifications()
        {
            List<SPWebConfigModification> configModifications = new List<SPWebConfigModification>();

            SPWebConfigModification handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            string EmlFormatHandlerType = typeof(EmlFormatHttpHandler).AssemblyQualifiedName;
            handlerRecord.Name = String.Format("add[@path='{0}']", handlerPath);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"EML\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
               handlerPath, EmlFormatHandlerType);

            configModifications.Add(handlerRecord);

            return configModifications;
        }
        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
